package com.shuai.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuai.mapper.SysOrganMapper;
import com.shuai.model.converter.SysOrganConverter;
import com.shuai.model.po.SysOrgan;
import com.shuai.model.po.SysRole;
import com.shuai.model.query.SysOrganQuery;
import com.shuai.model.query.SysRoleQuery;
import com.shuai.model.vo.SysOrganVo;
import com.shuai.service.SysOrganService;
import com.shuai.utils.converter.PageConverter;
import com.shuai.utils.converter.SystemConverter;
import com.shuai.utils.data.DataUtil;
import com.shuai.utils.tree.SysTreeVo;
import com.shuai.utils.tree.TreeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 机构信息 服务实现类
 *
 * @author shuai
 * @since 2022
 */
@Service
public class SysOrganServiceImpl extends ServiceImpl<SysOrganMapper, SysOrgan> implements SysOrganService {

    @Resource
    private SysOrganMapper sysOrganMapper;

    @Override
    public Integer addSysOrgan(SysOrgan sysOrgan) {
        return sysOrganMapper.insert(sysOrgan);
    }

    @Override
    public Integer deleteSysOrgan(SysOrganQuery query) {
        return sysOrganMapper.deleteBatchIds(query.getIds());
    }

    @Override
    public Integer editSysOrgan(SysOrgan sysOrgan) {
        return sysOrganMapper.updateById(sysOrgan);
    }

    @Override
    public List<SysOrganVo> selectList(SysOrganQuery query) {
        QueryWrapper<SysOrgan> queryWrapper = new QueryWrapper();
        fillQueryWrapper(queryWrapper, query);
        List<SysOrgan> organList = sysOrganMapper.selectList(queryWrapper);
        return SysOrganConverter.convertToVoByPo(organList);
    }

    @Override
    public IPage<SysOrganVo> selectPage(SysOrganQuery query) {
        QueryWrapper<SysOrgan> queryWrapper = new QueryWrapper();
        fillQueryWrapper(queryWrapper, query);
        Page<SysOrgan> page = new Page<>(query.getPage(), query.getLimit());
        IPage<SysOrgan> pageList = sysOrganMapper.selectPage(page, queryWrapper);
        List<SysOrganVo> records = SysOrganConverter.convertToVoByPo(pageList.getRecords());
        return PageConverter.convertToPage(pageList, records);
    }

    @Override
    public SysOrganVo selectById(Long id) {
        SysOrgan organ = sysOrganMapper.selectById(id);
        return SysOrganConverter.convertToVoByPo(organ);
    }

    @Override
    public List<SysTreeVo> selectTreeTree(SysOrganQuery query) {
        QueryWrapper<SysOrgan> queryWrapper = new QueryWrapper();
        fillQueryWrapper(queryWrapper, query);
        List<SysOrgan> organList = sysOrganMapper.selectList(queryWrapper);
        List<SysTreeVo> treeVoList = TreeUtil.makeTreeForSysTree(SystemConverter.convertToVoByPo(organList), "#");
        return treeVoList;
    }

    @Override
    public List<SysOrganVo> selectTreeList(SysOrganQuery query) {
        QueryWrapper<SysOrgan> queryWrapper = new QueryWrapper();
        fillQueryWrapper(queryWrapper, query);
        List<SysOrgan> organList = sysOrganMapper.selectList(queryWrapper);
        List<SysOrganVo> organVoList = TreeUtil.makeTreeForAny(SysOrganConverter.convertToVoByPo(organList), query.getNodeIdEq());
        return organVoList;
    }

    /**
     * 生成查询条件与排序
     *
     * @param query
     * @return
     */
    private void fillQueryWrapper(QueryWrapper<SysOrgan> queryWrapper, SysOrganQuery query) {
        //组织查询条件
        queryWrapper.lambda()
                .like(StrUtil.isNotEmpty(query.getNodeIdEq()),SysOrgan::getLabelPath,query.getNodeIdEq())
                .like(StrUtil.isNotEmpty(query.getLabelLike()), SysOrgan::getLabel, query.getLabelLike())
        ;

        //生成order by 的语句
        DataUtil.fillOrderBy(query, queryWrapper, SysOrgan.class);
    }

}
